import {
  decryptPhone,
  userInfo
} from '../../apis/user';
const app = getApp();

Page({
  data: {
    phone: ''
  },

  onLoad() {
    this.getUserInfo();
  },

  // 获取个人信息
  getUserInfo() {
    userInfo().then(res => {
      if (res.data.code === 200) {
        let {
          user
        } = res.data.data;
        this.setData({
          phone: user.phone
        });
      };
    });
  },

  // 获取手机号
  bindgetphonenumber(e) {
    if (e.detail.encryptedData) {
      this.decryptPhone(e.detail.encryptedData, e.detail.iv);
    };
  },

  // 解密手机号
  decryptPhone(encryptedData, iv) {
    decryptPhone(encryptedData, iv).then(res => {
      if (res.data.code === 200) {
        let {
          phone
        } = res.data.data.user;
        this.setData({
          phone: phone
        });
      };
    });
  },

  editUserInfo() {
    if (this.data.phone) {
      wx.navigateTo({
        url: '/pages/editUserInfo/index?type=user'
      });
    } else {
      wx.showToast({
        title: '请先绑定手机号',
        duration: 1500,
        icon: 'none'
      });
    };
  }
})